package com.hongpu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hongpu.config.SecurityContextUtil;
import com.hongpu.domain.HpProjectWorkPay;
import com.hongpu.domain.HpProjectWorkPayList;
import com.hongpu.domain.ProjectManager;
import com.hongpu.enums.CompanyTypeEnum;
import com.hongpu.enums.ProjectTypeEnum;
import com.hongpu.enums.ResultStatusEnum;
import com.hongpu.mapper.ProjectManagerMapper;
import com.hongpu.service.HpProjectWorkPayListService;
import com.hongpu.service.HpProjectWorkPayService;
import com.hongpu.service.ProjectManagerService;
import com.hongpu.utils.ResultDto;
import com.hongpu.vo.CompanyTypeVo;
import com.hongpu.vo.ProjectManagerSelectVo;
import com.hongpu.vo.ProjectManagerVo;
import com.hongpu.vo.ProjectTypeVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
* @author Michael zhang
* @description 针对表【project_manager(项目管理)】的数据库操作Service实现
* @createDate 2025-04-27 11:31:30
*/
@Service
public class ProjectManagerServiceImpl extends ServiceImpl<ProjectManagerMapper, ProjectManager>
    implements ProjectManagerService{
    @Autowired
    ProjectManagerMapper projectManagerMapper;
    @Autowired
    HpProjectWorkPayService hpProjectWorkPayService;
    @Autowired
    HpProjectWorkPayListService hpProjectWorkPayListService;


    @Override
    public IPage<ProjectManagerVo> selectPage(IPage<?> page, ProjectManager projectManager) {
        IPage<ProjectManagerVo> selectPage = projectManagerMapper.selectPage(page, projectManager);
        if(!CollectionUtils.isEmpty(selectPage.getRecords())){
            List<ProjectManagerVo> records = selectPage.getRecords();
            for (ProjectManagerVo projectManagerVo : records) {
                // 项目类型
                for (ProjectTypeEnum projectTypeEnum : ProjectTypeEnum.values()) {
                    if(projectTypeEnum.getValue().equals(projectManagerVo.getType())){
                        projectManagerVo.setType(projectTypeEnum.getValue());
                        projectManagerVo.setTypeName(projectTypeEnum.getDesc());
                    }
                }
                // 公司类型
                for (CompanyTypeEnum companyTypeEnum : CompanyTypeEnum.values()) {
                    if(companyTypeEnum.getValue().equals(projectManagerVo.getCompanyType())){
                        projectManagerVo.setCompanyType(companyTypeEnum.getValue());
                        projectManagerVo.setCompanyTypeName(companyTypeEnum.getDesc());
                    }
                }

            }
        }
        return selectPage;
    }

    /**
     * 查询所有
     *
     * @param projectManager
     * @return
     */
    @Override
    public ResultDto list(ProjectManager projectManager) {
       try  {
           List<ProjectManagerVo> list = baseMapper.list(projectManager);
           return ResultDto.ok(list);
       } catch (Exception e) {
           e.printStackTrace();
           return ResultDto.fail("查询失败");
       }

    }

    @Override
    public List<ProjectManagerSelectVo> optionList() {
        List<ProjectManager> list = list();
        List<ProjectManagerSelectVo> projectManagerVos = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)){
            for (ProjectManager projectManager : list) {
                ProjectManagerSelectVo projectManagerSelectVo = new ProjectManagerSelectVo();
                BeanUtils.copyProperties(projectManager,projectManagerSelectVo);
                projectManagerVos.add(projectManagerSelectVo);
            }
        }
        return projectManagerVos;
    }

    @Override
    public List<ProjectTypeVo> projectType() {
        List<ProjectTypeVo> list = new ArrayList<>();
        for (ProjectTypeEnum projectTypeEnum : ProjectTypeEnum.values()) {
            ProjectTypeVo projectTypeVo = new ProjectTypeVo();
            projectTypeVo.setValue(projectTypeEnum.getValue());
            projectTypeVo.setLabel(projectTypeEnum.getDesc());
            list.add(projectTypeVo);
        }
        return list;
    }

    @Override
    public List<CompanyTypeVo> companyType() {
        List<CompanyTypeVo> list = new ArrayList<>();
        for (CompanyTypeEnum companyTypeEnum : CompanyTypeEnum.values()) {
            CompanyTypeVo companyTypeVo = new CompanyTypeVo();
            companyTypeVo.setValue(companyTypeEnum.getValue());
            companyTypeVo.setLabel(companyTypeEnum.getDesc());
            list.add(companyTypeVo);
        }
        return list;
    }

    /**
     * 查询项目名称是否重复
     *
     * @param projectName
     * @return
     */
    @Override
    public boolean selectByProjectName(String name) {
        QueryWrapper<ProjectManager> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        List<ProjectManager> projectManagers = baseMapper.selectList(queryWrapper);
        if(!CollectionUtils.isEmpty(projectManagers)){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 删除项目，在删除前需要验证是否存在关联数据
     *
     * @param id
     */
    @Override
    public ResultDto deleteById(Long id) {
        if(id == null){
            return ResultDto.fail(ResultStatusEnum.FAIL_DELETE_PARAM.getMsgCode(),ResultStatusEnum.FAIL_DELETE_PARAM.getMessage());
        }
        QueryWrapper<HpProjectWorkPay> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id",id);
        List<HpProjectWorkPay> list = hpProjectWorkPayService.list(queryWrapper);
        // 根据list中数据的id查询明细表是否有数据
        List<Long> ids = new ArrayList<>();
        for (HpProjectWorkPay hpProjectWorkPay : list) {
            ids.add(hpProjectWorkPay.getId());
        }
        if(!CollectionUtils.isEmpty(ids)){
            QueryWrapper<HpProjectWorkPayList> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.in("project_work_pay_id",ids);
            List<HpProjectWorkPayList> hpProjectWorkPayLists = hpProjectWorkPayListService.list(queryWrapper1);
            if(CollectionUtils.isEmpty(hpProjectWorkPayLists)){
                // 收支主表删除
                hpProjectWorkPayService.removeBatchByIds(ids);
                // 项目删除
                removeById(id);
                return ResultDto.ok();
            }else {
                return ResultDto.fail(ResultStatusEnum.FAIL_DELETE_USED.getMsgCode(),ResultStatusEnum.FAIL_DELETE_USED.getMessage());
            }
        }else {
            // 删除项目
            removeById(id);
            return ResultDto.ok();
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultDto add(ProjectManager projectManager) {
        if(StringUtils.isEmpty(projectManager.getName())){
            return ResultDto.fail("项目名称不能为空");
        }

        try {
            Long tenantId = SecurityContextUtil.getTenantId();
            projectManager.setOurCompanyId(tenantId);
            save(projectManager);
            hpProjectWorkPayService.addProjectWorkPay(projectManager.getId(),projectManager.getName(),projectManager.getProjectAmount());
            return ResultDto.ok();
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }

    /**
     * 修改项目信息
     *
     * @param projectManager
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultDto update(ProjectManager projectManager) {
        try {
            // 修改项目信息
            updateById(projectManager);
            hpProjectWorkPayService.updateProjectName(projectManager.getId(),projectManager.getName(),projectManager.getProjectAmount());
            return ResultDto.ok();
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }
}




